<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/6/28
 * Time: 15:18
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Privatesetmeal extends Base
{
    /**
     * 套餐首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $where['ps_status'] = ['NEQ', 3];
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('private_setmeal')->where($where)->count();// 获取总条数
            $lists = Db::name('private_setmeal')
                ->where($where)
                ->field('ps_id, ps_name, ps_image, ps_price, ps_vip_price, ps_spec, ps_xg, ps_status, 
                ps_time, ps_update_time, ps_db_img, ps_detail_img, ps_pf_img, ps_mj_img')
                ->page($NowPage,$limits)
                ->order('ps_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加套餐
     * @return mixed|\think\response\Json
     */
    public function setmealAdd(){
        if(Request::instance()->isPost()){
            $ps_name = trim(input('ps_name'), ' ');
            $ps_image = input('ps_image');
            $ps_images = input('ps_images');
            $ps_status = input('ps_status');
            $ps_content = input('ps_content');
            $ps_price = input('ps_price');
            $ps_vip_price = input('ps_vip_price');
            $ps_spec = input('ps_spec');
            $ps_xg = input('ps_xg');
            $ps_db_img = input('ps_db_img');
            $ps_detail_img = input('ps_detail_img');
            $ps_pf_img = input('ps_pf_img');
            $ps_mj_img = input('ps_mj_img');
            
            $insert = [
                'ps_name' => $ps_name,
                'ps_image' => $ps_image,
                'ps_images' => $ps_images,
                'ps_content' => $ps_content,
                'ps_status' => $ps_status,
                'ps_price' => $ps_price,
                'ps_vip_price' => $ps_vip_price,
                'ps_spec' => $ps_spec,
                'ps_xg' => $ps_xg,
                'ps_db_img' => $ps_db_img,
                'ps_detail_img' => $ps_detail_img,
                'ps_pf_img' => $ps_pf_img,
                'ps_mj_img' => $ps_mj_img,
                'ps_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_private_setmeal')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('privatesetmeal/setmealadd');
    }
    
    /**
     * 套餐详情
     * @return mixed
     */
    public function setmealDetail(){
        $ps_id = input('ps_id');
        $setmeal = Db::table('up_private_setmeal')
            ->where('ps_id', $ps_id)
            ->field('ps_name, ps_image, ps_images, ps_content, ps_status, ps_price, ps_vip_price, 
            ps_spec, ps_xg, ps_db_img, ps_detail_img, ps_pf_img, ps_mj_img')
            ->find();
        if($setmeal['ps_images']){
            $setmeal['ps_images'] = explode(',', $setmeal['ps_images']);
        }else{
            $setmeal['ps_images'] = [];
        }
        return $this->fetch('privatesetmeal/setmealdetail', ['setmeal' => $setmeal]);
    }
    
    /**
     * 编辑套餐
     * @return mixed|\think\response\Json
     */
    public function setmealEdit(){
        $ps_id = input('ps_id');
        if(Request::instance()->isPost()){
            $ps_name = trim(input('ps_name'), ' ');
            $ps_image = input('ps_image');
            $ps_images = input('ps_images');
            $ps_status = input('ps_status');
            $ps_content = input('ps_content');
            $ps_price = input('ps_price');
            $ps_vip_price = input('ps_vip_price');
            $ps_spec = input('ps_spec');
            $ps_xg = input('ps_xg');
            $ps_db_img = input('ps_db_img');
            $ps_detail_img = input('ps_detail_img');
            $ps_pf_img = input('ps_pf_img');
            $ps_mj_img = input('ps_mj_img');
            
            $update = [
                'ps_name' => $ps_name,
                'ps_image' => $ps_image,
                'ps_content' => $ps_content,
                'ps_status' => $ps_status,
                'ps_price' => $ps_price,
                'ps_vip_price' => $ps_vip_price,
                'ps_spec' => $ps_spec,
                'ps_xg' => $ps_xg,
                'ps_db_img' => $ps_db_img,
                'ps_detail_img' => $ps_detail_img,
                'ps_pf_img' => $ps_pf_img,
                'ps_mj_img' => $ps_mj_img,
                'ps_update_time' => date('Y-m-d H:i:s')
            ];
            if($ps_images) $update['ps_images'] = $ps_images;
            
            $res = Db::table('up_private_setmeal')->where('ps_id', $ps_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $setmeal = Db::table('up_private_setmeal')
            ->where('ps_id', $ps_id)
            ->field('ps_name, ps_image, ps_images, ps_content, ps_status, ps_price, ps_vip_price, 
            ps_spec, ps_xg, ps_db_img, ps_detail_img, ps_pf_img, ps_mj_img')
            ->find();
        $this->assign([
            'ps_id' => $ps_id,
            'setmeal' => $setmeal
        ]);
        return $this->fetch('privatesetmeal/setmealedit');
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function setmealDown(){
        $ps_id = input('id');
        $res = Db::table('up_private_setmeal')->where('ps_id', $ps_id)->setField('ps_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function setmealOn(){
        $ps_id = input('id');
        $res = Db::table('up_private_setmeal')->where('ps_id', $ps_id)->setField('ps_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function setmealDel(){
        $ps_id = input('id');
        $res = Db::table('up_private_setmeal')->where('ps_id', $ps_id)->setField('ps_status' , 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}